﻿using CPPEI.Coolzon.UI.XProperty;
using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Convertors
{
    public class XSeriesTypeDefConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            SeriesTypeDef std = new SeriesTypeDef();
            string XSeriesTypeDef = valueNode.Attributes["Value"];
            if (!string.IsNullOrEmpty(XSeriesTypeDef))
            {
                string[] strs = XSeriesTypeDef.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string str in strs)
                {
                    string[] seriesType = str.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if(seriesType.Length >= 2)
                    {
                        std[seriesType[0]] = seriesType[1];
                    }
                }
            }
            return std;
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            SeriesTypeDef std = (SeriesTypeDef)value;
            if(std == null)
            {
                return null;
            }
            Metadata.ValueNode valueNode = new Metadata.ValueNode("SeriesTypeDef");
            valueNode.Attributes = new Dictionary<string, string>();
            string XSeriesTypeDef = string.Empty;
            foreach(var item in std)
            {
                XSeriesTypeDef += string.Format("{0}:{1}," + item.Key, item.Value);
            }
            XSeriesTypeDef = XSeriesTypeDef.TrimEnd(',');
            valueNode.Attributes.Add("Value", XSeriesTypeDef.ToString());

            return valueNode;
        }
    }
}
